package com.jsmile.cloud.authc.sys.repo;

import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.authc.api.req.sys.ReqSysLogQuery;
import com.jsmile.cloud.authc.sys.dao.SysLogDao;
import com.jsmile.cloud.authc.sys.model.SysLog;
import com.jsmile.mall.api.kit.DateKit;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年10月16日 02:46:20
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SysLogSearchRepository extends ServiceImpl<SysLogDao, SysLog> {

    public IPage<SysLog> pageQuery(ReqSysLogQuery reqSysLogQuery) {
        return this.baseMapper.selectPage(reqSysLogQuery.getPage(), getQueryWrapper(reqSysLogQuery));
    }

    private Wrapper getQueryWrapper(ReqSysLogQuery reqSysLogQuery) {
        return new QueryWrapper<SysLog>().lambda().eq(null != reqSysLogQuery.getCurTenantId(), SysLog::getTenantId, reqSysLogQuery.getCurTenantId())
            .like(Objects.nonNull(reqSysLogQuery.getTitle()), SysLog::getTitle, reqSysLogQuery.getTitle())
            .like(Objects.nonNull(reqSysLogQuery.getRemark()), SysLog::getRemark, reqSysLogQuery.getRemark())
            .like(Objects.nonNull(reqSysLogQuery.getUserName()), SysLog::getUserName, reqSysLogQuery.getUserName())
            .ge(null != reqSysLogQuery.getStartDate(), SysLog::getCreateTime, DateKit.beginOfDay(reqSysLogQuery.getStartDate()))
            .le(null != reqSysLogQuery.getEndDate(), SysLog::getCreateTime, DateKit.endOfDay(reqSysLogQuery.getEndDate())).orderByDesc(SysLog::getCreateTime);// sort,createTime倒序
    }
}
